'''
Created on Jun 14, 2010

@author: oabalbin
'''
import sys
from optparse import OptionParser
from datetime import datetime

import interactome.build_interactome.extract_network as ite
import interactome.output.write_files as of


if __name__ == '__main__':
    optionparser = OptionParser("usage: %prog [options] ")
    optionparser.add_option("-t", "--transcriptsFile", dest="transcriptsFile",
                        help="annotation file for all files to use")
    optionparser.add_option("-p", "--protsFile", dest="protsFile",
                        help="annotation file for all files to use")    
    optionparser.add_option("-d", "--outfolder", dest="outfolder",
                        help="output folder")

    (options, args) = optionparser.parse_args()
    
    t = datetime.now()
    tstamp = t.strftime("%Y_%m_%d_%H_%M")
    
    outfiles = ['dhea_edges', 'dhea_nodes','dhea_nodes_number','cytoscape_net','dhea_nodes2']
    time_stamp_outfiles=[]
    for name in outfiles: 
        time_stamp_outfiles.append( options.outfolder+tstamp+name+'.txt')
        #time_stamp_outfiles.append( options.outfolder+name+'.txt')

    # Start main program
    myInt = ite.myInteractome()
    # Score used to select relevant interactions
    interaction_score = ['experimental',500]#500] combined_score 
    ### Build the Interactome
    # Get list proteins in network
    #inputfile_genes = '/data/projects/pcst/test/AR_interactome_test/AR_gene_list'
    inputfile_genes =  '/data/projects/pcst/test/Phospho_interactome_test/phospho_rasonly_sigprots_string.txt'
    initial_gene_list = of.list_of_names(open(inputfile_genes))
    trans2prot_edge=True
    
    
    list_of_proteins, list_of_proteins_revIndex, notfound, prot2genes_table = myInt.get_proteins_in_network(initial_gene_list)
    print "Number of query proteins "+ str(len(list_of_proteins))
    # Get interactions between proteins in network
    list_of_interactions = myInt.get_interactions_in_network(list_of_proteins, trans2prot_edge)
    print "Number of extracted interactions "+ str(len(list_of_interactions))
    # Select the interactions with high confidence
    # you could also use the function select_interactions_by_score
    selected_interactions, selected_nodes = myInt.select_interactions_by_scoreS8(list_of_interactions, interaction_score)
    print "Number of selected nodes and interactions "+  str(len(selected_nodes)), str(len(selected_interactions))
    
    ### write files
    outputfile_dhea_edges=open(time_stamp_outfiles[0],'w')
    outputfile_dhea_nodes=open(time_stamp_outfiles[1],'w')
    outputfile_dhea_nodes_number=open(time_stamp_outfiles[2],'w')
    outputfile_cytoscape=open(time_stamp_outfiles[3],'w')
    outputfile_dhea_nodes2=open(time_stamp_outfiles[4],'w')
    
    # Input file with the weigth for each node
    inputfile_trasn = open(options.transcriptsFile) 
    inputfile_prots = open(options.protsFile)
    genes_of_interest = of.dict_of_names(inputfile_trasn)
    prots_of_interest = of.dict_of_names(inputfile_prots)
    
    nodelistDict = of.map_nodes_names2number(selected_nodes)
    of.write_edge_list_dhea_file2namesS8(selected_interactions, nodelistDict, outputfile_dhea_edges, interaction_score[0])
    of.write_nodes_list_dhea_file(selected_nodes,nodelistDict,outputfile_dhea_nodes, nodes_score=False)
    of.write_nodes_list_dhea_file2namesS8(selected_nodes, nodelistDict, prots_of_interest,genes_of_interest, outputfile_dhea_nodes2)
    of.write_node_list_numberS8(nodelistDict, outputfile_dhea_nodes_number, list_of_proteins_revIndex)
    
    '''
    of.write_edge_list_dhea_file2names2(selected_interactions, list_of_proteins_revIndex, nodelistDict, outputfile_dhea_edges, interaction_score[0])
    of.write_nodes_list_dhea_file(selected_nodes,nodelistDict,outputfile_dhea_nodes, nodes_score=False)
    #of.write_nodes_list_dhea_file2names(selected_nodes, list_of_proteins_revIndex, nodelistDict, genes_of_interest, outputfile_dhea_nodes2)
    of.write_nodes_list_dhea_file2names2(selected_nodes, list_of_proteins_revIndex, nodelistDict, prots_of_interest,genes_of_interest, outputfile_dhea_nodes2)
    of.write_node_list_number(nodelistDict, outputfile_dhea_nodes_number, list_of_proteins_revIndex)
    of.write_edge_list_cytoscape(selected_interactions, list_of_proteins_revIndex,outputfile_cytoscape, interaction_score[0])
    '''
